package com.etc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.etc.ResultBody;
import com.etc.entity.SysUser;
import com.etc.enums.ResultStatusEnums;
import com.etc.repository.SysUserRepository;
import com.etc.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 武晓春
 * @Description用户业务层
 * @Date 2021/2/19  18:24
 **/
@Service
public class SysUserServiceImpl implements ISysUserService {
    @Autowired
    private SysUserRepository sysUserRepository;
//   登录
    @Override
    public ResultBody login(String username, String password) {
       if (StrUtil.isBlank(username)){
          return ResultBody.error(ResultStatusEnums.USERNAME_IS_NULL);
       }if (StrUtil.isBlank(password)){
           return ResultBody.error(ResultStatusEnums.PASSWORD_IS_NULL);
        }
        List<SysUser> byUsername = sysUserRepository.findByUsername(username);
       if (byUsername!=null&&!byUsername.isEmpty()){
           SysUser sysUser = byUsername.get(0);
           if (sysUser.getPassword().equals(password)){
               return ResultBody.ok(sysUser);
           }else{
              return ResultBody.error(ResultStatusEnums.WRONG_PASSWORD);
           }
       }else {
           return ResultBody.error(ResultStatusEnums.USER_DOES_NOT_EXIST);
       }
    }
}
